Internet Connection

Internet Connection :-

Internet Connection class

public static String getResponseString(String requestURL) { URL url; String response = ""; try { url = new URL(requestURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(35000); conn.setConnectTimeout(35000); /*conn.setReadTimeout(100); conn.setConnectTimeout(100);*/ conn.setRequestMethod("GET"); conn.setDoInput(true); conn.setDoOutput(true); int responseCode=conn.getResponseCode(); if (responseCode == HttpsURLConnection.HTTP_OK) { String line; BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line=br.readLine()) != null) { response+=line; } } else { response=""; } } catch (Exception e) { e.printStackTrace(); } return response; } public static final ProgressDialog getProgressDialog(Context context, String dialogMessage) { final ProgressDialog progressDialog = new ProgressDialog(context); progressDialog.setCancelable(false); progressDialog.setMessage(dialogMessage); return progressDialog; } public static void RedirectToActivity(Activity yourActivity, Class SecondActivity, boolean isfinish, Bundle b) { Intent intent = new Intent(yourActivity, SecondActivity); if (b != null) intent.putExtras(b); yourActivity.startActivity(intent); yourActivity.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right); if (isfinish) { yourActivity.finish(); } } public static Dialog setDialog(Context context) { dialog = new Dialog(context); dialog.setCancelable(false); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.dialog_loading); dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); return dialog; }

Async Class

package app.googlemap.Response; import android.os.AsyncTask; import android.util.Log; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import javax.net.ssl.HttpsURLConnection; import app.googlemap.activity.GoogleApi; public class BackgroudAsyn extends AsyncTask { String url = "https://maps.googleapis.com/maps/api/place/nearbysearch/" + "json?location=-33.8670,151.1957&radius=500&types=food&name=crui" + "se&key=AIzaSyD7g423k39xJjr6UraNDN8s2XjnkQIbD-Y"; GoogleApi googleApi; @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(Object... params) { String response = null; googleApi = (GoogleApi) params[0]; HashMap postDataParams = new HashMap(); response = getResponseStringUsingPostMethod(url, postDataParams); Log.d("Response ..", "doInBackground: " + response); return response; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); // GoogleApi googleApi = new GoogleApi(); googleApi.api(s); } public static String getResponseStringUsingPostMethod(String requestURL, HashMap postDataParams) { URL url; String response = ""; try { url = new URL(requestURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(35000); conn.setConnectTimeout(35000); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8")); writer.write(getPostDataString(postDataParams)); writer.flush(); writer.close(); os.close(); int responseCode=conn.getResponseCode(); if (responseCode == HttpsURLConnection.HTTP_OK) { String line; BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line=br.readLine()) != null) { response+=line; } } else { response=""; } } catch (Exception e) { e.printStackTrace(); } return response; } private static String getPostDataString(HashMap params) throws UnsupportedEncodingException { StringBuilder result = new StringBuilder(); boolean first = true; for(Map.Entry entry : params.entrySet()){ if (first) first = false; else result.append("&"); result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); } Log.d("post string", "post string==" + result.toString()); return result.toString(); } }

Http Class Method

public class HTTPWebRequest { public static void Registration(Context context, RegistrationRequest req,int apiCode, ApiResponse apiResponse) { HashMap postDataParams = new HashMap(); postDataParams.put(AppConstants.RequestDataKey.FIRSTNAME, req.getFirstname()); /*postDataParams.put(AppConstants.RequestDataKey.LASTNAME, req.getLastname());*/ postDataParams.put(AppConstants.RequestDataKey.EMAIL, req.getEmail()); postDataParams.put(AppConstants.RequestDataKey.PASSWORD, req.getPassword()); postDataParams.put(AppConstants.RequestDataKey.ADDRESS, req.getAddress()); postDataParams.put(AppConstants.RequestDataKey.ADDRESS_2, req.getStreet()); postDataParams.put(AppConstants.RequestDataKey.ZIPCODE, req.getZipcode()); postDataParams.put(AppConstants.RequestDataKey.CONTACT, req.getContact()); new BackgroundAsyncTask(context , APIURL.URL_REGISTER_USER ,DialogMessage.PLEASE_WAIT ,apiCode,postDataParams,false).execute(apiResponse); } public static void Login(Context context, LoginRequest req,int apiCode, ApiResponse apiResponse) { HashMap postDataParams = new HashMap(); postDataParams.put(AppConstants.RequestDataKey.EMAIL, req.getEmail()); postDataParams.put(AppConstants.RequestDataKey.PASSWORD, req.getPassword()); new BackgroundAsyncTask(context , APIURL.URL_LOGIN_USER ,DialogMessage.PLEASE_WAIT , apiCode,postDataParams,false).execute(apiResponse); } public static void GetProductInfo(Context context,int apiCode, ApiResponse apiResponse) { HashMap postDataParams = new HashMap(); new BackgroundAsyncTask(context ,APIURL.URL_GET_PRODUCTINFO,DialogMessage.PLEASE_WAIT, apiCode,postDataParams,false).execute(apiResponse); } } public void apiResponsePostProcessing(String response, int apiCode) { if(response != null) { Gson gson = new Gson(); try { switch (apiCode) { case AppConstants.APICode.REGISTER_USER: RegistrationResponse registrationResponse = gson.fromJson(response,RegistrationResponse.class); if(registrationResponse.getStatus() ==1) { Toast.makeText(RegisterActivity.this, "" + registrationResponse.getMessage(), Toast.LENGTH_SHORT).show(); KissKiss.preferencePutBoolean(AppConstants.SharedPreferenceKeys.IS_LOGGED_IN, true); Utility.RedirectToActivity(RegisterActivity.this, AccountCreatedActivity.class, true, null); } else { Toast.makeText(RegisterActivity.this,""+registrationResponse.getMessage(),Toast.LENGTH_SHORT).show(); } break; } }catch(Exception e){ Log.e("RegistrationActivity", "Error In Response:-" + e); } } }